---
import AnimationLayout from "#/layouts/AnimationLayout.astro"
import type { LearningPageFields } from "#/lib/contentful/types.ts"
import DeepDiveSection from "../components/sections/DeepDiveSection.astro"
import { contentfulClient } from "../lib/contentful/client.ts"

export const prerender = false

let learningData: LearningPageFields | null = null
let error: string | null = null
try {
  const landingEntries = await contentfulClient.getEntries<LearningPageFields>({
    content_type: "learn",
  })

  if (landingEntries.items.length > 0) {
    learningData = landingEntries.items[0].fields
  } else {
    error = "No learning page data found."
  }
} catch (err) {
  console.error("Error fetching Contentful data:", err)
  error = "Failed to load content. Please try again later."
}
---

{
  error ? (
    <div class="error-message">
      <h2>Error</h2>
      <p>{error}</p>
    </div>
  ) : learningData ? (
    <AnimationLayout
      title="Learn"
      text="Study Union."
    >
      <DeepDiveSection entries={learningData} />
    </AnimationLayout>
  ) : (
    <div class="loading-message">
      <p>Loading content...</p>
    </div>
  )
}
